package com.tofine.smarthome.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tofine.smarthome.R;
import com.tofine.smarthome.model.Room;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author ddMax
 * @since 2016/5/26 14:12
 * 说明：设备添加时房间选择的Adapter
 */
public class DeviceAddToRoomAdapter extends RecyclerView.Adapter<DeviceAddToRoomAdapter.ViewHolder> {

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_add_room_group) RelativeLayout mRoomItem;
        @Bind(R.id.room_title) TextView mRoomTitle;
        @Bind(R.id.rb_room) RadioButton mRadioButton;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private Context mContext;
    private List<Room> mDataSet;
    private RadioButton lastCheckedRadioButton;
    private int selection = -1;

    public DeviceAddToRoomAdapter(Context context, List<Room> dataSet) {
        this.mContext = context;
        this.mDataSet = dataSet;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_add_room_group, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        Room room = mDataSet.get(position);
        holder.mRoomTitle.setText(room.name);
        holder.mRoomItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.mRadioButton.isChecked()) {
                    // 当前RadioButton已选中
                } else {
                    // 未选中
                    if (lastCheckedRadioButton != null) {
                        lastCheckedRadioButton.setChecked(false);
                    }
                    lastCheckedRadioButton = holder.mRadioButton;
                    holder.mRadioButton.setChecked(true);
                }
                selection = position;
                Log.d(DeviceAddToRoomAdapter.class.getSimpleName(), "" + getSelection());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataSet.size();
    }

    public int getSelection() {
        return selection;
    }
}
